/**
 * @param {number[][]} intervals
 * @param {number[]} newInterval
 * @return {number[][]}
 */
var insert = function(intervals, newInterval) {
    if(intervals.length === 0) return [newInterval]
    intervals.push(newInterval)
    intervals.sort((a, b) =>  a[0] - b[0])
    const res = [intervals]
    let ptr = 0
    for(let i = 1; i < intervals.length; i++) {
        if(intervals[i][0] > res[ptr][1]) {
            res.push(intervals[i])
            ptr++
        } else {
            res[ptr] = [res[ptr][0], Math.max(res[ptr][1], intervals[i][1])]
        }
    }
    return res
};